﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MIMSDomain.Abstract;
using MIMSDomain.Entities;
using Moq;

namespace MIMSUnitTests.Domain
{
    [TestClass]
    public class EFGenotypeRepositoryTests
    {
        public IRepository<Genotype> MockGenotypeRepository;

        [TestInitialize]
        public void Initialize()
        {
            //Arrange
            var _genotypes = new List<Genotype> {new Genotype{ Id=1, Name="AS"},
            new Genotype{ Id=2, Name="AA"}};

            var target = new Mock<IRepository<Genotype>>();
            target.Setup(p => p.All).Returns(_genotypes.AsQueryable());
            target.Setup(p => p.GetById(It.IsAny<int>())).Returns((int i) =>
                _genotypes.Where(p => p.Id == i).Single());
            target.Setup(p => p.Save(It.IsAny<Genotype>())).Returns((Genotype objGenotype) =>
                {
                    if (objGenotype.Id.Equals(default(int)))
                    {
                        objGenotype.Id = _genotypes.Count() + 1;
                        _genotypes.Add(objGenotype);
                    }
                    else
                    {
                        var original = _genotypes.Where(p => p.Id == objGenotype.Id).Single();
                        if (original == null)
                        {
                            return false;
                        }
                        original.Name = objGenotype.Name;
                    }
                    return true;
                });
            target.Setup(p => p.Delete(It.IsAny<Genotype>())).Returns((Genotype objGenotype) =>
                {
                    if (_genotypes.Contains(objGenotype))
                    {
                        _genotypes.Remove(objGenotype);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                });
            MockGenotypeRepository = target.Object;
        }

        [TestMethod]
        public void CanSaveGenotype()
        {
            //Arrange
            var count = MockGenotypeRepository.All.Count();
            //Assert
            Assert.AreEqual(2, count); //Confirm genotype count before saving
            //Act
            var expected = MockGenotypeRepository.Save(new Genotype { Name = "SS" });

            //Assert
            count = MockGenotypeRepository.All.Count();
            Assert.AreEqual(3, count); //Confirm genotype count after saving
        }
        [TestMethod]
        public void CanDeleteGenotype()
        {
            //Arrange
            var count = MockGenotypeRepository.All.Count();
            Assert.AreEqual(2, count);
            var objGenotype = MockGenotypeRepository.GetById(2);
            Assert.IsInstanceOfType(objGenotype, typeof(Genotype));
            //Act
            MockGenotypeRepository.Delete(objGenotype);
            count = MockGenotypeRepository.All.Count();
            //Assert
            Assert.AreEqual(1, count);
        }
        [TestMethod]
        public void CanGetGenotypeById()
        {
            //Act
            var actual = MockGenotypeRepository.GetById(2);
            //Assert
            Assert.IsNotNull(actual);
            Assert.IsInstanceOfType(actual, typeof(Genotype));
            Assert.AreEqual("AA", actual.Name);
        }
        [TestMethod]
        public void CanGetAllGenotypes()
        {
            //Act
            var expected = MockGenotypeRepository.All;
            //Assert
            Assert.IsNotNull(expected);
            Assert.IsInstanceOfType(expected, typeof(IQueryable<Genotype>));
            Assert.AreEqual(expected.Count(), 2);
        }
        [TestMethod]
        public void CannotAddDuplicateGenotype()
        {
            throw new NotImplementedException();
        }
    }
}
